package com.boco.blch.analysiser.compliance.HW.manager;

import com.boco.blch.analysiser.compliance.ComplianceResolved;

/**
 * YARN&MR审计日志文件权限最小化安全基线要求项【控制节点】+【数据节点】
 * 
 * 【控制节点】
 * 1、查看 日志文件配置
 * /opt/huawei/bigdata/etc/2_21_ResourceManager/log4j.properties的如下配置
 * log4j.appender.MRAUDIT.File的值的目录下的所有文件；
 * 2、查看 文件权限 是否 是  600
 * 
 * 
 * 【数据节点】
 * 1、查看 日志文件配置
 * /opt/huawei/bigdata/etc/2_21_ResourceManager/log4j.properties的如下配置
 * log4j.appender.MRAUDIT.File的值的目录下的所有文件；
 * 2、查看 文件权限 是否 是  600
 * @author admin
 *
 */
public class BOCO_Yarn_LogAudit_AuditLogFileMinAuth implements ComplianceResolved {
	
	private final static String EXPECT_RESULT = "-rw-------";
	
	@Override
	public int realEchoResolved(String echoResult)  {
		
		int result = FAILED;
		if(echoResult != null && echoResult.length() > 0){
			if(echoResult.indexOf("NodeManager-start") != -1){//数据节点
				String strResult = echoResult.substring(echoResult.indexOf("NodeManager-start")+"NodeManager-start".length(), echoResult.indexOf("NodeManager-end")).trim();
				result = getResult(strResult);
			}
			if(result == FAILED && echoResult.indexOf("ResourceManager-start") != -1){//控制节点
				String strResult = echoResult.substring(echoResult.indexOf("ResourceManager-start")+"ResourceManager-start".length(), echoResult.indexOf("ResourceManager-end")).trim();
				result = getResult(strResult);
			}
			if(result == FAILED && echoResult.indexOf("JobHistoryServer-start") != -1){//控制节点
				String strResult = echoResult.substring(echoResult.indexOf("JobHistoryServer-start")+"JobHistoryServer-start".length(), echoResult.indexOf("JobHistoryServer-end")).trim();
				result = getResult(strResult);
			}
		}
		return result;
	}
	
	private int getResult(String echoResult){
		int result = FAILED;
//		if(echoResult == null || echoResult.length() <= 0 || echoResult.contains(NotFindFile) 
//				|| echoResult.contains(NoPermission2) || echoResult.contains(NoPermission3)){
//			result = FAILED;
//		}else{
		String []str1 = echoResult.split("\n");
		for (int i = 0; i < str1.length; i++) {
			//String str2 = str1[i].substring(0, str1[i].lastIndexOf("-"));
			String str = str1[i];
			if(str.indexOf("-r")>-1){
				System.out.println(str);
				boolean flag = str.contains(EXPECT_RESULT);
				if(flag){
					result=SUCCESS;
				}else{
					result = FAILED;
					break;
				}
				System.out.println("日志目录下的文件的权限是否为600："+flag);
			}
		}
//		}
		return result;
	}
	
	

}
